//
//  PictorialCell.m
//  Pictorial
//
//  Created by SKY on 15/12/29.
//  Copyright © 2015年 sky. All rights reserved.
//

#import "PictorialCell.h"
#import <UIImageView+WebCache.h>

#define PICTURE_HEIGHT  120
#define PICTURE_WIDTH_MAX   (PICTURE_HEIGHT * 0.75 + 5)

@interface PictorialCell ()<UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UILabel *titleCLabel;
@property (weak, nonatomic) IBOutlet UILabel *titleELabel;
@property (weak, nonatomic) IBOutlet UILabel *countLabel;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@property (nonatomic,strong) NSMutableArray *dataArray;

@property (nonatomic,strong) NSMutableArray *indexArray;

@end
@implementation PictorialCell

- (void)awakeFromNib {
    // Initialization code
    self.scrollView.delegate = self;
}
-(void)setModel:(PictorialModel *)model{
    _model = model;
    
    self.titleCLabel.text = model.destination[@"name_zh_cn"];
    self.titleELabel.text = model.destination[@"name_en"];
    self.countLabel.text = [NSString stringWithFormat:@"%ld组画报", model.articles.count];
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat w = PICTURE_HEIGHT * 0.75;
    CGFloat h = PICTURE_HEIGHT;
    CGFloat interval = 5;
    
    for (int index = 0; index < model.articles.count; index ++) {
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(touchPicture:)];
        
        UIImageView *imageView = [UIImageView imageViewWithLocalImage:@"placholder.png" frame:CGRectMake(x + (w + interval) * index, y, w, h)];
        [self.scrollView addSubview:imageView];
        
        [self.dataArray addObject:imageView];
        
        imageView.userInteractionEnabled = YES;
        imageView.tag = index + 100;
        [imageView addGestureRecognizer:tap];
    }

    self.scrollView.contentSize = CGSizeMake((w + interval) * model.articles.count + interval, self.scrollView.frame.size.height);
    [self loadData:0];
}
-(void)loadData:(NSInteger)number{
    
    for (int index = 0; index < self.indexArray.count; index ++) {
        if ([[NSString stringWithFormat:@"%ld",number] isEqualToString:self.indexArray[index]]) {
            return;
        }
    }
    NSInteger count = WIDTH / PICTURE_WIDTH_MAX + 1;
    
    NSInteger lastNumber = count;
    if (self.model.articles.count - number * count < count) {
        lastNumber = self.model.articles.count - number * count;
    }
    
    for (NSInteger index = number * count; index < number * count + lastNumber; index ++) {
        
        PictorialArticleModel *articleModel = self.model.articles[index];
        
        UIImageView *imageView = self.dataArray[index];
        [imageView imageLoadProgressWithNetImage:articleModel.image_url];
    }
    
    [self.indexArray addObject:[NSString stringWithFormat:@"%ld",number]];
}
#pragma mark - ScrollView
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    NSInteger count = WIDTH / PICTURE_WIDTH_MAX + 1;

    NSInteger number = scrollView.contentOffset.x / PICTURE_WIDTH_MAX / count;
    [self loadData:number];
}
- (void)touchPicture:(UITapGestureRecognizer *)tap{
    
    PictorialArticleModel *articleModel = self.model.articles[tap.view.tag - 100];
    [_delegate moveToDetailVC:articleModel];
    
}
-(NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
-(NSMutableArray *)indexArray{
    if (_indexArray == nil) {
        _indexArray = [NSMutableArray array];
    }
    return _indexArray;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
